import 'package:flutter/material.dart';

/// 通用输入对话框
class InputDialog extends StatefulWidget {
  /// 对话框标题
  final String title;

  /// 文本输入提示
  final String hint;

  /// 初始文本
  final String? initialValue;

  /// 确认按钮文本
  final String confirmText;

  /// 取消按钮文本
  final String cancelText;

  /// 文本验证回调，返回null表示验证通过，否则返回错误信息
  final String? Function(String)? validator;

  /// 确认回调函数，返回用户输入的值
  final Function(String)? onConfirm;

  const InputDialog({
    Key? key,
    required this.title,
    required this.hint,
    this.initialValue,
    this.confirmText = '确定',
    this.cancelText = '取消',
    this.validator,
    this.onConfirm,
  }) : super(key: key);

  /// 显示输入对话框的静态方法
  static Future<String?> show({
    required BuildContext context,
    required String title,
    required String hint,
    String? initialValue,
    String confirmText = '确定',
    String cancelText = '取消',
    String? Function(String)? validator,
    Function(String)? onConfirm,
  }) {
    return showDialog<String>(
      context: context,
      builder:
          (context) => InputDialog(
            title: title,
            hint: hint,
            initialValue: initialValue,
            confirmText: confirmText,
            cancelText: cancelText,
            validator: validator,
            onConfirm: (value) {
              Navigator.of(context).pop(value);
              if (onConfirm != null) {
                onConfirm(value);
              }
            },
          ),
    );
  }

  @override
  State<InputDialog> createState() => _InputDialogState();
}

class _InputDialogState extends State<InputDialog> {
  late TextEditingController _controller;
  String? _errorText;
  bool _isValid = true;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController(text: widget.initialValue);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  /// 验证输入
  void _validate(String value) {
    if (widget.validator != null) {
      setState(() {
        _errorText = widget.validator!(value);
        _isValid = _errorText == null;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text(widget.title),
      content: TextField(
        controller: _controller,
        autofocus: true,
        decoration: InputDecoration(
          hintText: widget.hint,
          border: const OutlineInputBorder(),
          errorText: _errorText,
        ),
        onChanged: _validate,
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(context).pop(),
          child: Text(widget.cancelText),
        ),
        ElevatedButton(
          onPressed:
              !_isValid || _controller.text.trim().isEmpty
                  ? null
                  : () {
                    if (widget.onConfirm != null) {
                      widget.onConfirm!(_controller.text.trim());
                    }
                  },
          child: Text(widget.confirmText),
        ),
      ],
    );
  }
}
